﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class InGameGui : MonoBehaviour {

    private AudioSource sound;//声音组件
    public GameObject gameOn , gameOver;//游戏开始，结束界面
    public Text score, best, ingameScore, pointText;//结束分数，最佳分数，游戏分数，结束点数
    public Color[] medalCols;//奖牌颜色数组
    public Image medal;//奖牌图片控件
    public Button homeBtn, retryBtn;//主页按钮，重开按钮
    public string mainMenu;//主页场景名称
    private bool isReward;//是否结算
	
	void Start ()
    {
        sound = GetComponent<AudioSource>();
        GameManager.instance.currentScore = 0;
        ingameScore.text = "" + GameManager.instance.currentScore;
        homeBtn.GetComponent<Button>().onClick.AddListener(() => { HomeBtn(); });    //home       
        retryBtn.GetComponent<Button>().onClick.AddListener(() => { RetryBtn(); });    //retry   
    }

    void Update ()
    {
        ingameScore.text = "" + GameManager.instance.currentScore;

        if (GameManager.instance.currentScore >= GameManager.instance.hiScore)
        {
            GameManager.instance.hiScore = GameManager.instance.currentScore;
            GameManager.instance.Save();
        }
        //游戏结束
        if (GameManager.instance.isGameOver && !isReward)
        {
            score.text = "" + GameManager.instance.currentScore;
            best.text = "" + GameManager.instance.hiScore;
            MedalColor();
            gameOn.SetActive(false);
            gameOver.SetActive(true);         
            int point = GameManager.instance.currentScore / 10;
            pointText.text = "+" + point;
            GameManager.instance.points += point;
            GameManager.instance.Save();
            isReward = true;
        }

    }

    //主页按钮
    void HomeBtn()
    {
        sound.Play();
        GameManager.instance.isGameOver = false;
        SceneManager.LoadScene(mainMenu);
    }

    //重开按钮
    void RetryBtn()
    {
        sound.Play();
        GameManager.instance.isGameOver = false;
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }

   //奖牌颜色
    void MedalColor()
    {
        if (GameManager.instance.currentScore >= 10)
        {
            medal.color = medalCols[0];
        }
        else if (GameManager.instance.currentScore >= 25)
        {
            medal.color = medalCols[1];
        }
        else if (GameManager.instance.currentScore >= 40)
        {
            medal.color = medalCols[2];
        }
        else if (GameManager.instance.currentScore >= 60)
        {
            medal.color = medalCols[3];
        }
        else if (GameManager.instance.currentScore >= 80)
        {
            medal.color = medalCols[4];
        }
    }


}
